package ar.com.ford.it.fullEconomics.struct;

import java.math.BigDecimal;
import java.util.Vector;

public class AgreementReportTable {

    private Vector<AgreementReportVehicleLine> vehicleLines;
    private AgreementReportRow generalAverage;
    private BigDecimal totalVolume;

    public AgreementReportTable() {
        vehicleLines = new Vector<AgreementReportVehicleLine>();
        generalAverage = new AgreementReportRow();
        totalVolume = new BigDecimal(0);
    }

    public boolean addVehicleLine(AgreementReportVehicleLine vehicleLine) {
        return vehicleLines.add(vehicleLine);
    }

    public void calculateGeneralTotals() {
        // sumo los totales por linea multiplicados por su respectivo columen
        for (AgreementReportVehicleLine vehicleLine : vehicleLines) {
            totalVolume = totalVolume.add(vehicleLine.getVolume());
            generalAverage.setCurrPrice(generalAverage.getCurrPrice().add(
                    vehicleLine.getLineAverage().getCurrPrice().multiply(
                            vehicleLine.getVolume())));
            generalAverage.setFulleAmount(generalAverage.getFulleAmount().add(
                    vehicleLine.getLineAverage().getFulleAmount().multiply(
                            vehicleLine.getVolume())));
            generalAverage.setRequestAmount(generalAverage.getRequestAmount()
                    .add(
                            vehicleLine.getLineAverage().getRequestAmount()
                                    .multiply(vehicleLine.getVolume())));
            generalAverage.setPayPolAmount(generalAverage.getPayPolAmount()
                    .add(
                            vehicleLine.getLineAverage().getPayPolAmount()
                                    .multiply(vehicleLine.getVolume())));
            generalAverage.setLastAgreemAmount(generalAverage
                    .getLastAgreemAmount().add(
                            vehicleLine.getLineAverage().getLastAgreemAmount()
                                    .multiply(vehicleLine.getVolume())));
        }
        // divido cada total para el volumne total
        generalAverage.setCurrPrice(generalAverage.getCurrPrice().divide(
                totalVolume, 5, BigDecimal.ROUND_HALF_EVEN));
        generalAverage.setFulleAmount(generalAverage.getFulleAmount().divide(
                totalVolume, 5, BigDecimal.ROUND_HALF_EVEN));
        generalAverage.setRequestAmount(generalAverage.getRequestAmount()
                .divide(totalVolume, 5, BigDecimal.ROUND_HALF_EVEN));
        generalAverage.setPayPolAmount(generalAverage.getPayPolAmount().divide(
                totalVolume, 5, BigDecimal.ROUND_HALF_EVEN));
        generalAverage.setLastAgreemAmount(generalAverage.getLastAgreemAmount()
                .divide(totalVolume, 5, BigDecimal.ROUND_HALF_EVEN));

        // calculo los porcentajes
        if (generalAverage.getFulleAmount().compareTo(new BigDecimal(0)) != 0)
            generalAverage.setFullePercent(generalAverage.getFulleAmount()
                    .subtract(generalAverage.getCurrPrice()).divide(
                            generalAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
        if (generalAverage.getRequestAmount().compareTo(new BigDecimal(0)) != 0)
            generalAverage.setRequestPercent(generalAverage.getRequestAmount()
                    .subtract(generalAverage.getCurrPrice()).divide(
                            generalAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
        if (generalAverage.getPayPolAmount().compareTo(new BigDecimal(0)) != 0)
            generalAverage.setPayPolPercent(generalAverage.getPayPolAmount()
                    .subtract(generalAverage.getCurrPrice()).divide(
                            generalAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
        if (generalAverage.getLastAgreemAmount().compareTo(new BigDecimal(0)) != 0)
            generalAverage.setLastAgreemPercent(generalAverage
                    .getLastAgreemAmount().subtract(
                            generalAverage.getCurrPrice()).divide(
                            generalAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
    }

    public AgreementReportRow getGeneralAverage() {
        return generalAverage;
    }

    public void setGeneralAverage(AgreementReportRow generalAverage) {
        this.generalAverage = generalAverage;
    }

    public Vector<AgreementReportVehicleLine> getVehicleLines() {
        return vehicleLines;
    }

    public void setVehicleLines(Vector<AgreementReportVehicleLine> vehicleLines) {
        this.vehicleLines = vehicleLines;
    }

}
